<?php
	require_once(realpath(dirname(__FILE__)) . "/Baseobj.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Produitdesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Stock.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Image.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Document.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Reecriture.class.php");

	class Produit extends Baseobj{

		var $id;
		var $ref;
		var $datemodif;
		var $prix;
		var $ecotaxe;
		var $promo;
		var $ligne;
		var $garantie;
		var $prix2;
		var $rubrique;
		var $nouveaute;
		var $perso;
		var $stock;
		var $poids;
		var $tva;
		var $classement;

		const TABLE="produit";
		var $table=self::TABLE;

		var $bddvars=array("id", "ref", "datemodif", "prix", "ecotaxe", "promo", "ligne", "garantie", "prix2", "rubrique", "nouveaute", "perso", "stock", "poids", "tva", "classement");

		function __construct($ref = ""){
			parent::__construct();

			if($ref != "")
 			  $this->charger($ref);
		}

		function charger($ref){
			return $this->getVars("select * from $this->table where ref=\"$ref\"");
		}

		function charger_id($id){
			return $this->getVars("select * from $this->table where id=\"$id\"");
		}

		function changer_classement($ref, $type){

			$this->charger($ref);
			$remplace = new Produit();

			if($type == "M")
				$res = $remplace->getVars("select * from $this->table where rubrique=\"" . $this->rubrique. "\" and classement<" . $this->classement . " order by classement desc limit 0,1");

			else if($type == "D")
				$res  = $remplace->getVars("select * from $this->table where rubrique=\"" . $this->rubrique. "\" and classement>" . $this->classement . " order by classement limit 0,1");

			if(! $res)
				return "";

			$sauv = $remplace->classement;

			$remplace->classement = $this->classement;
			$this->classement = $sauv;

            $remplace->maj();
            $this->maj();

		}

		function delete($requete){
				$resul = mysql_query($requete, $this->link);
				CacheBase::getCache()->reset_cache();
				
		}

		function supprimer(){

			if ($this->id == 0 || $this->id == "") return;

			$stock = new Stock();
			$query = "delete from $stock->table where produit='" . $this->id . "'";
			$resul = mysql_query($query, $stock->link);

			$image = new Image();
			
			$query = "select * from $image->table where produit=\"" . $this->id . "\" AND dossier=\"0\" AND contenu=\"0\" AND rubrique=\"0\" ";
			$resul = mysql_query($query, $image->link);
			while($row = mysql_fetch_object($resul)){
				$tmp = new Image();
				$tmp->charger($row->id);
				$tmp->supprimer();

			}

			$document = new Document();

			$query = "select * from $document->table where produit=\"" . $this->id . "\" AND dossier=\"0\" AND contenu=\"0\" AND rubrique=\"0\" ";
			$resul = mysql_query($query, $document->link);
			while($row = mysql_fetch_object($resul)){
				$tmp = new Document();
				$tmp->charger($row->id);
				$tmp->supprimer();

			}
			
			$query = 'select id from '.$this->table.' where rubrique='.$this->rubrique.' and classement>'.$this->classement;
			$resul = mysql_query($query,$this->link);
			
			if(mysql_num_rows($resul)>0){
			  $query_classement = 'update '.$this->table.' set classement=(classement-1) where rubrique='.$this->rubrique.' and classement>'.$this->classement;
			  $resul_classement = mysql_query($query_classement,$this->link);
			}
      

			$produitdesc =  new Produitdesc();

			$this->delete("delete from $this->table where id=\"$this->id\"");
			$this->delete("delete from $produitdesc->table where produit=\"$this->id\"");

			return 1;

		}


	}

?>